package com.haojie.client;

import com.haojie.codec.MyDecoder;
import com.haojie.codec.MyEncoder;
import com.haojie.codec.ObjectSerializer1;
import com.haojie.server.ServiceProvider;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;

public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {

    ServiceProvider serviceProvider;
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
        pipeline.addLast(new LengthFieldPrepender(4));
        pipeline.addLast(new MyEncoder(new ObjectSerializer1()));
        pipeline.addLast(new MyDecoder());
        pipeline.addLast(new NettyClientHandler(serviceProvider));

    }
}
